<?php

	include_once("$config[webroot]/includes/page_utf_class.php");
	
	//初始化品牌话题
	function init_brand_tag($brandId,$tag_name,$logo){
		global $db;
		$sql = "select id from ".SNSTAGS." where (tagname='{$tag_name}' or brandid='{$brandId}') and status=1";
		$db->query($sql);
		$tagId = $db->fetchField('id');
		if($tagId>0){
			$sql = "update ".SNSTAGS." set logo='{$logo}',brandid='{$brandId}',brand='{$tag_name}' where id='{$tagId}'";
		}else{
			$sql = "insert into ".SNSTAGS." (tagname,logo,brandid,brand) values ('{$tag_name}','{$logo}','{$brandId}','{$tag_name}')";
		}
		$db->query($sql);
	}
	//检查品牌是否已经存在
	if($_GET['operation']=="checkBrand" && $_GET['brandname']){
		$sql = "select id from ".BRAND." where name='".mysql_escape_string($_GET['brandname'])."' and id<>'{$_GET['brandid']}'";
		$db->query($sql);
		$brandId = $db->fetchField('id');
		if($brandId>0){
			die(json_encode(array("error"=>1,"msg"=>"品牌已经存在！")));
		}
		exit;
	}
	if($_GET['operation']=="add" or $_GET['operation']=="edit" or $_GET['operation']=='batch')
	{
		if($_POST['act'])
		{	
			unset($_GET['operation']);
			unset($_GET['s']);
			unset($_GET['m']);
			$_POST['status']=empty($_POST['status'])?0:$_POST['status'];
			$_POST['logo']=empty($_POST['logo'])?'':$_POST['logo'];
			$_POST['catid']=empty($_POST['catid'])?0:$_POST['catid'];
			$catid = $_POST['sub_catid']>0?$_POST['sub_catid']:$_POST['catid'];
			include_once($config['webroot'].'/lib/allchar.php');
			$str=c(trim($_POST['name']));
			$fstr=substr($str,0,1);
			
			$sql="select id,catname from ".BRANDCAT." where id= {$catid} limit 1";
			$db->query($sql);
			$cat=$db->fetchRow();
			
			
			$pic='';
			$pic_more='';
			$img='';
			$_POST['pic']=array_unique($_POST['pic']);
			if(!empty($_POST['pic']))
			{
			    foreach($_POST['pic'] as $val)
			    {
			        if($val)
			        {
			            $img[]=$val;
			        }
			    }
			    if(!empty($img))
			    {
			        $pic=$img['0'];
			        $pic_more=implode(',',array_unique($img));
			    }
			}
			
			
			
			
	
			if($_POST["act"]=='batch')
			{
				foreach(explode("\r\n",$_POST['name']) as $na)
				{
					$str=c(trim($na));
					$fstr=substr($str,0,1);
					$sql="insert into ".BRAND." (name,catid,catname,logo,status,create_user,char_index,pic,story) values ('$na','$catid','$cat[catname]','".$_POST['logo']."','".$_POST['status']."','$_SESSION[ADMIN_USER]','$fstr','$pic_more','$_POST[story]')";
					$bid = $db->query($sql);
					if($bid>0){
						init_brand_tag($bid,$na, $_POST['logo']);
					}
				}	
			}
			//添加
			if($_POST["act"]=='save')
			{
				$sql="insert into ".BRAND." (name,seo_name,catid,catname,logo,logo_large,status,create_user,char_index,pic,story) values 
			('$_POST[name]','$_POST[seo_name]','$catid','$cat[catname]','".$_POST['logo']."','".$_POST['logo_large']."','".$_POST['status']."','$_SESSION[ADMIN_USER]','$fstr','$pic_more','$_POST[story]')";
				$bid = $db->query($sql);
				if($bid>0){
					init_brand_tag($bid,$_POST['name'], $_POST['logo']);
				}
			}
			//修改
			if($_POST["act"]=='edit' and is_numeric($_POST['id']))
			{
				$sql="update ".BRAND." set name='".$_POST['name']."',seo_name='".$_POST['seo_name']."',catid='$catid',catname='$cat[catname]',logo='".$_POST['logo']."',logo_large='".$_POST['logo_large']."',status='".$_POST['status']."',char_index='$fstr',pic='$pic_more',story='$_POST[story]'
			where id='".$_POST['id']."'";
				$db->query($sql);
				init_brand_tag($_GET['editid'],$_POST['name'], $_POST['logo']);
				unset($_GET['editid']);
			}
			$getstr=implode('&',convert($_GET));
			msg("?m=brand&s=brand.php$getstr");
		}
		//信息
		if($_GET['editid'] and is_numeric($_GET['editid']))
		{
			$sql="select * from ".BRAND." where id='$_GET[editid]'";
			$db->query($sql);
			$de=$db->fetchRow();
			$de['pic'] = $de['pic']?explode(',',$de['pic']):'';
		}
		
		//获取分类
		$sql="select id,catname from ".BRANDCAT." where parent_id=0 order by displayorder";
		$db->query($sql);
		$cat=$db->getRows();
		foreach($cat as $key=>$val)
		{
			$sql="select * from ".BRANDCAT." where parent_id='$val[id]' order by displayorder";
			$db->query($sql);
			$cat[$key]['scat']=$db->getRows();	
		}
		$tpl->assign("cat",$cat);
		$tpl->assign("district",GetDistrict());
		$tpl->assign("config",$config);
	}
	else
	{
		//删除
		if($_GET['delid'])
		{
			$sql="delete from ".BRAND."  where id='$_GET[delid]'";
			$db->query($sql);
			unset($_GET['delid']);
			unset($_GET['s']);
			unset($_GET['m']);
			msg("?m=brand&s=brand.php$getstr");
		}
		if($_POST['act']=='op')
		{
			if($_POST['submit']==$lang['btn_submit'])
			{
				if(is_array($_POST['chk']))
				{
					$id=implode(",",$_POST['chk']);
					$sql="delete from ".BRAND." where id in ($id)";
					$db->query($sql);
				}
				if($_POST['displayorder'])
				{
					foreach($_POST['displayorder'] as $key=>$list)
					{
						$db->query("update ".BRAND." set displayorder='$list' where id='$key'");		
					}
				}
			}
			else
			{
				if(is_array($_POST['chk']))
				{
					foreach($_POST['chk'] as $val)
					{
						if($_POST['submit']==$lang['rc'])
						{
							$db->query("update ".BRAND." set status='2' where id='$val'");
						}
						elseif($_POST['submit']==$lang['btn_open'])
						{
							$db->query("update ".BRAND." set status='1' where id='$val'");
						}
						elseif($_POST['submit']==$lang['btn_close'])
						{
							$db->query("update ".BRAND." set status='0' where id='$val'");
						}
					}
				}
			}
			msg("?m=brand&s=brand.php$getstr");
		}	
		//获取
		$sql="select b.*,c.catname from ".BRAND." b left join ".BRANDCAT." c on c.id=b.catid order by displayorder , id  desc ";
		//=============================
		$page = new Page;
		$page->listRows=20;
		if (!$page->__get('totalRows')){
			$db->query($sql);
			$page->totalRows = $db->num_rows();
		}
		$sql .= "  limit ".$page->firstRow.",20";
		$pages = $page->prompt();
		//=====================
		$db->query($sql);
		$de['list']=$db->getRows();
		$de['page']=$page->prompt();
	}
	$tpl->assign("de",$de);
	$tpl->display("brand.htm");

?>